package xin.sunzy.ssms.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import xin.sunzy.ssms.entity.po.Valid;
import xin.sunzy.ssms.service.intf.IValidService;

@Controller
public class MyController {

    private Logger logger = LoggerFactory.getLogger(MyController.class);

    @Autowired
    private IValidService validService;

    @ResponseBody
    @RequestMapping("/test")
    public Object test(){
        return "<h2>hello world</h2>";
    }

    @ResponseBody
    @RequestMapping("/valid")
    public Object valid(){
        try {
            return validService.getValidById(1);
        } catch (Exception e) {
            return new Valid("404","失败");
        }
    }

    @RequestMapping("/login")
    public String login(@RequestParam("username")String username, @RequestParam("password") String password){
        Subject currentUser = SecurityUtils.getSubject();
        if(!currentUser.isAuthenticated()){
            UsernamePasswordToken token = new UsernamePasswordToken(username,password);
            token.setRememberMe(false);
            try {
                currentUser.login(token);
            }catch (Exception e){
                logger.error("登陆异常");
                return "login";
            }
        }
        return "redirect:/index.jsp";
    }
}
